package com.goodluck.goodluckim.ui.session.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;
import com.goodluck.goodluckim.R;
import com.goodluck.goodluckim.db.model.RecentSession;
import com.goodluck.goodluckim.sdk.constant.SessionType;
import com.goodluck.goodluckim.sdk.event.SessionEvent;
import com.goodluck.goodluckim.ui.msg.activity.FragmentActivity;
import com.goodluck.goodluckim.ui.msg.fragment.MessageFragment;
import com.goodluck.goodluckim.ui.session.adapter.SessionAdapter;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.thread.EventThread;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * Created by wyj on 2018/9/12.
 */

public class RecentSessionFragment extends Fragment {
    private RecyclerView mSessionRv;
    private SessionAdapter mAdapter;
    private List<RecentSession> mRecentSessions;

    public RecentSessionFragment() {
    }

    public static RecentSessionFragment newInstance() {
        RecentSessionFragment fragment = new RecentSessionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.fragment_session_list, container, false);
        mRecentSessions = new ArrayList<>();
        mAdapter = new SessionAdapter(mRecentSessions);
        mSessionRv = (RecyclerView) mainView.findViewById(R.id.session_rv);
        mSessionRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSessionRv.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(0xffededed).size(2).build());
        mSessionRv.setAdapter(mAdapter);

        for (int i = 0; i < 100; i++) {
            RecentSession recentSession = new RecentSession();
            int sessionType = new Random().nextInt(4);
            if (sessionType == 0) {
                sessionType = 1;
            }

            recentSession.setSessionType(sessionType);

            int msgType = new Random().nextInt(7);
            if (msgType == 0) {
                msgType = 1;
            }
            if (msgType == 1) {
                recentSession.setMessageContent("会话内容：asdagg[ecq]gdj[eci]fsdfsdf[eex][ecg][deg]" + i);
            }
            recentSession.setMessageType(msgType);
            recentSession.setObjectName((sessionType == 2 ? "群聊" : "") + "会话标题" + i);
            mRecentSessions.add(recentSession);
        }
        mAdapter.notifyDataSetChanged();

        mSessionRv.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RecentSession recentSession = (RecentSession) adapter.getItem(position);
                switch (recentSession.getSessionType()) {
                    case SessionType.TEAM:
                        FragmentActivity.goToFragment(getActivity(), MessageFragment.class, null);
                        break;
                    default:
                        Toast.makeText(getActivity(), "会话类型：" + recentSession.getSessionType(), Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        return mainView;
    }

    @Subscribe(thread = EventThread.MAIN_THREAD)
    public void onSessionEvent(SessionEvent sessionEvent) {
        switch (sessionEvent.getEventType()) {
            case SessionEvent.EventType.ADD_SESSSION:
                mRecentSessions.add(sessionEvent.getRecentSession());
                sort();
                break;
            case SessionEvent.EventType.UPDATE_SESSION:
                for (int i = 0; i < mRecentSessions.size(); i++) {
                    RecentSession recentSession = mRecentSessions.get(i);
                    if (sessionEvent.getRecentSession().getSessionID() == recentSession.getSessionID()) {
                        recentSession.setMessageContent(sessionEvent.getRecentSession().getMessageContent());
                        recentSession.setExtra(sessionEvent.getRecentSession().getExtra());
                        recentSession.setReceivedTime(sessionEvent.getRecentSession().getReceivedTime());
                        recentSession.setReceiveId(sessionEvent.getRecentSession().getReceiveId());
                        recentSession.setSenderUserId(sessionEvent.getRecentSession().getSenderUserId());
                        recentSession.setSendTime(sessionEvent.getRecentSession().getSendTime());
                        recentSession.setReadReceiptInfo(recentSession.getReadReceiptInfo() + sessionEvent.getRecentSession().getReadReceiptInfo());
                        sort();
                        mAdapter.refreshNotifyItemChanged(i);
                        break;
                    }
                }
                break;
            default:

                break;
        }
    }

    /**
     * 排序
     */
    private void sort() {
        Collections.sort(mRecentSessions, new Comparator<RecentSession>() {
            @Override
            public int compare(RecentSession o1, RecentSession o2) {
                return 0;
            }
        });
    }
}
